package com.cskaoyan.market.admin.controller;

import com.cskaoyan.market.admin.service.impl.MarketKeywordServiceImpl;
import com.cskaoyan.market.db.domain.MarketKeyword;
import com.cskaoyan.market.util.GetReqBodyUtil;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/admin/keyword")
public class AdminKeywordController{
    @Autowired
    private MarketKeywordServiceImpl marketKeywordService;
    
    /*
     * 查找逻辑：
     * 如果keyword为空或者没有设置，则查询所有
     * 查询的问题为关键字查询，包含查询语句的所有问题都会查出
     * 关键：如果被逻辑删除的数据不会被显示出来
     * */
    @GetMapping("/list")
    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String keyword = req.getParameter("keyword");
        List<MarketKeyword> marketKeywordList = marketKeywordService.list(keyword, Integer.parseInt(page), Integer.parseInt(limit), sort, order);

        //keyword为空或null,查找所有问题
        List<MarketKeyword> realList1 = new ArrayList<>();

        //keyword不为空或null,判断keyword是否存在或者被数据库中keyword包含
        List<MarketKeyword> realList2 = new ArrayList<>();

        for (MarketKeyword marketKeyword : marketKeywordList) {
            String realKeyword = marketKeyword.getKeyword();
            if (keyword == null || keyword.equals("")) {
                realList1.add(marketKeyword);
            } else if (realKeyword.contains(keyword)) {
                realList2.add(marketKeyword);
            }
        }

        if (realList1.size() != 0) {
            Object o1 = ResponseUtil.okList(realList1);
            ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
            String s = objectMapper.writeValueAsString(o1);
            resp.getWriter().println(s);
            return;
        }
        if (realList2.size() != 0) {
            Object o1 = ResponseUtil.okList(realList2);
            ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
            String s = objectMapper.writeValueAsString(o1);
            resp.getWriter().println(s);
        } else {
            //keyword不包含且不存在
            Object o1 = ResponseUtil.ok();
            ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
            String s = objectMapper.writeValueAsString(o1);
            resp.getWriter().println(s);
        }
    }

    /*
     * 增加逻辑：
     * 解析请求体中的keyword、isDefault、isHot
     * 写到MarketKeyword对象中
     * 将MarketKeyword对象转化成JSON对象响应出去
     * */
    @PostMapping("/create")
    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //请求参数位于请求体中，所以我们需要自己主动的去解析请求体
        String requestBody = GetReqBodyUtil.getRequestBody(req);
        String keyword = JacksonUtil.parseString(requestBody, "keyword");
        boolean isDefault = Boolean.parseBoolean(JacksonUtil.parseString(requestBody, "isDefault"));
        boolean isHot = Boolean.parseBoolean(JacksonUtil.parseString(requestBody, "isHot"));
        MarketKeyword marketKeyword = marketKeywordService.create(keyword, isHot, isDefault);
        Object ok = ResponseUtil.ok(marketKeyword);
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        String s = objectMapper.writeValueAsString(ok);
        resp.getWriter().println(s);

    }

    /*
     * 修改逻辑：
     * 解析请求体中的keyword、isDefault、isHot以及id
     * 根据id查找要修改的对象来修改
     * */
    @PostMapping("/update")
    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //请求参数位于请求体中，所以我们需要自己主动的去解析请求体
        String requestBody = GetReqBodyUtil.getRequestBody(req);
        String keyword = JacksonUtil.parseString(requestBody, "keyword");
        String url = JacksonUtil.parseString(requestBody, "url");
        boolean isDefault = Boolean.parseBoolean(JacksonUtil.parseString(requestBody, "isDefault"));
        boolean isHot = Boolean.parseBoolean(JacksonUtil.parseString(requestBody, "isHot"));
        String id = JacksonUtil.parseString(requestBody, "id");
        MarketKeyword marketKeyword = marketKeywordService.update(Integer.parseInt(id), keyword, url, isDefault, isHot);
        Object ok = ResponseUtil.ok(marketKeyword);
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        String s = objectMapper.writeValueAsString(ok);
        resp.getWriter().println(s);
    }

    /*
     * 删除逻辑：
     * 解析请求体中的id
     * 根据id对对应的对象进行逻辑删除
     * */
    @PostMapping("/delete")
    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //请求参数位于请求体中，所以我们需要自己主动的去解析请求体
        String requestBody = GetReqBodyUtil.getRequestBody(req);
        String id = JacksonUtil.parseString(requestBody, "id");
        marketKeywordService.delete(Integer.parseInt(id));
        Object ok = ResponseUtil.ok();
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        String s = objectMapper.writeValueAsString(ok);
        resp.getWriter().println(s);
    }
}